package org.liyufeng.project.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import org.liyufeng.project.model.PayrollPersonal;
import org.liyufeng.project.dao.PayrollPersonalDao;
import org.liyufeng.project.dao.impl.PayrollPersonalDaoImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PayrollServlet extends HttpServlet {
	private static final long serialVersionUID = 6790029477091312226L;
	private static Logger log = LoggerFactory.getLogger(PayrollServlet.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		
		PrintWriter out = response.getWriter();

		String method = request.getParameter("method");
		String UserId = request.getParameter("UserID");

		PayrollPersonalDao payrollPersonalDao = new PayrollPersonalDaoImpl(); // DAO
		log.info("UserId:{}",UserId);
		
		if (method.equals("getAll")) {
			ArrayList<PayrollPersonal> payrollPersonal = payrollPersonalDao.getAll(UserId);
			String jsonPayrollPersonal = JSONArray.fromObject(payrollPersonal).toString();
			out.write(jsonPayrollPersonal);
		} 
		out.flush();
		out.close();
	}
}
